<div id="orders-breakdown">
  <div class="row">
    <div>
      <label>Total Orders</label>
      <%= @order_count %>
    </div>
    <div>
      <label>Paid</label>
      <%= @paid_count %>
    </div>
    <div>
      <label>Free</label>
      <%= @free_count %>
    </div>
  </div>

  <div class="row">
    <div>
      <label>Total Revenue</label>
      <%= "$%.2f" % [@total_revenue] %>
    </div>

    <div>
      <label>Net to Author</label>
      <%= "$%.2f" % [Order.completed.author_total] %>
    </div>

    <div>
      <label>Total Donations</label>
      <%= "$%.2f" % [@total_donations] %>
    </div>

    <div>
      <label>Fees</label>
      <%= "$%.2f" % @total_fees %>
    </div>
  </div>

  <div class="row">
    <div>
      <label>Average Price Paid</label>
      <%= "$%.2f" % Order.completed.avg(:amount, :conditions => ['amount != ?', 0.0]) %>
    </div>


    <div>
      <label>Average Donation</label>
      <%= "$%.2f" % [@total_donations / @paid_count] %>
    </div>

    <div>
      <label>Biggest Purchase</label>
      <%= "$%.2f" % @max_purchase %>
    </div>
  </div>

  <div class="row">
    <div>
      <label>Purchased with Stripe</label>
      <%= Order.completed.count(:stripe_id.not => nil, :amount.not => 0.0) %>
    </div>

    <div>
      <label>Purchased with Paypal</label>
      <%= Order.completed.count(:paypal_token.not => nil, :amount.not => 0.0) %>
    </div>
  </div>

  <div class="row">
    <h4>Amount to be Donated: <%= "$%.2f" % Order.completed.sum(:donation_amount, :conditions => ['donated != ?', 'true']) %></h4>
    <form action="/admin/mark-paid" method="post">
      <input type="submit" class="submit-button" value="Mark As Donated">
    </form>
  </div>
</div>

<h3>Last 200 Orders</h3>

<table id="order-table">
  <thead>
    <th>To Author</th>
    <th>Donation</th>
    <th>Order Email</th>
    <th>Date</th>
    <th>Stripe or Paypal</th>
  </thead>
  <tbody>
<% @orders.each do |order| %>
  <tr>
    <td><%= "$%.2f" % order.amount %></td>
    <td><%= "$%.2f" % ((order.donation / 100.0 * order.amount) || 0.0) %></td>
    <td><%= order.email %></td>
    <td><%= order.created_at %></td>
    <% if order.amount > 0 %>
    <td><%= order.stripe_id ? "Stripe" : "Paypal" %></td>
    <% else %>
    <td>-</td>
    <% end %>
  </tr>
<% end %>
</tbody>
<tfoot>

</tfoot>
</table>

<a href="/admin/orders.csv">Download orders as CSV</a>